package Tracker.http;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import org.json.JSONObject;

import com.sun.net.httpserver.*;

public class Response {
	JSONObject body;
	Headers headers;
	HttpExchange exchange;
	
	public Response(HttpExchange t) {
		this.exchange = t;
		this.headers = t.getResponseHeaders();
	}
	
	public void send(int status) throws IOException {
		this.exchange.sendResponseHeaders(status, this.body.length());
		OutputStream os = this.exchange.getResponseBody();
		os.write(this.body.toString().getBytes());
		os.close();
	}
	
	public String getHeader(String key) {
		return this.headers.getFirst(key);
	}
	
	public List<String> getHeaders(String key) {
		return this.headers.get(key);
	}
	
	public Headers getHeaders() {
		return this.headers;
	}
	
	public void setHeader(String key, String value) {
		this.headers.add(key, value);
	}
	
	public JSONObject getBody() {
		return this.body;
	}
}
